/*
 * @lc app=leetcode.cn id=309 lang=javascript
 *
 * [309] 买卖股票的最佳时机含冷冻期
 */

// @lc code=start
/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function (prices) {
  const n = prices.length;
  // 完成当天交易后，股票不同状态下的最大利润，【0,1,2】状态持有、冷却期、空仓
  const profits = Array.from({ length: n }, () => new Array(3).fill(0));
  profits[0][0] = -prices[0];
  for (let i = 1; i < n; i++) {
    profits[i][0] = Math.max(profits[i - 1][0], profits[i - 1][2] - prices[i]);
    profits[i][1] = profits[i - 1][0] + prices[i];
    profits[i][2] = Math.max(profits[i - 1][1], profits[i - 1][2]);
  }
  return Math.max(profits[n - 1][1], profits[n - 1][2]);
};
// @lc code=end
